#include <stdio.h>
#include <unistd.h>  //多进程命令 fork execve waitpid
#include <sys/types.h> // pid_t
#include <sys/wait.h> //waitpid



int main(int argc, char const *argv[])
{
    printf("父进程启动 %d \n",getpid());
    int val =100;

    pid_t pid= fork();//父进程pid是子进程的pid，子进程pid是0

    if(pid==0)
    {   // 子进程
        printf("子进程启动  %d \n" ,getpid());
        // char cmd[]="/usr/bin/ping"; //命令
        // char *argv[]={cmd,"-c","5","www.baidu.com",NULL}; //名称+参数
        // char *envp[]={NULL};//环境变量
        
        // execve(cmd,argv,envp);
        val ++;
        printf("子进程修改val为%d \n",val);
        sleep(3);
    }else{
        sleep(1);  //父
        printf("父进程启动 %d\n",getpid());
  
        printf("父进程修改val为%d \n",val);
        int status =0;
        pid_t ret_pid=  waitpid(pid,&status,0);
        printf("1 子进程退出状态:status :%d , ret_pid :%d\n",status,ret_pid);
        ret_pid=  waitpid(pid,&status,0);
        printf("2 子进程退出状态:status :%d , ret_pid :%d\n",status,ret_pid);
    }

    
    printf(" 进程结束 pid:%d ppid:%d ,\n",getpid(),getppid());
 

    return 0;
}
